package {
	import flash.display.BitmapData;
    import org.flixel.*;
  
    /**
     * ...
     * @author Richard Pan
     */
    public class MovingPlatform extends FlxSprite {
        
        private var id:Number;
        private var distance:Number;
        private var origX:Number;
        private var origY:Number;
        private var velocityX:Number;
        private var velocityY:Number;
        private var moved:Boolean;
        
        // Constructor to create a platform (rectangle)
        public function MovingPlatform(X:Number, Y:Number, Width:Number, Height:Number,
                                    VelocityX:Number, VelocityY:Number, distance:Number,
                                    id:Number, moved:Boolean) {
            // Create sprite at coordinate X, Y
            super(X, Y);

            origX = X;
            origY = Y;
            velocityX = VelocityX;
            velocityY = VelocityY;
            
            // bounding box
            width = Width;
            height = Height;
            
            this.id = id;
            this.distance = distance;
            this.moved = moved;

            immovable = true;
            
            pixels = new BitmapData(width, height, true, 0x00000000);
			framePixels = pixels;
			// Manipulate the corners
			for ( var i:int = 0; i < Width; i++ )
			{
				for ( var j:int = 0; j < Height; j++ )
				{
					// Generate color (we want some patterns to get a smeary effect)
					var Color:uint = (Math.random() * 16 + 4 * i) % 32;
					Color = Color << 8;
					Color = Color & 0x0000FF00;
					Color += (Math.random() * 16 + j) % 48;
					Color = Color << 8;
					Color = Color & 0x00FFFF00;
					Color += (Math.random() * 16 + i * j) % 64;
					Color &= 0x00FFFFFF;
						
					// Detect top-left
					if ( i < 3 && j < 3 )
					{
						var dist:Number = (i + j) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i >= Width-3 && j < 3 ) // Top-right
					{
						var iOff:int = Width - i - 1;
						var dist:Number = (iOff + j) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i < 3 && j >= Height-3 ) // Bottom-left
					{
						var jOff:int = Height - j - 1;
						var dist:Number = (i + jOff) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i >= Width-3 && j >= Height-3 ) // Bottom-left
					{
						var jOff:int = Height - j - 1;
						var iOff:int = Width - i - 1;
						var dist:Number = (iOff + jOff) / 5;
						var alpha:int = dist * 255;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i < 3 )			// Left side
					{
						var dist:Number = i * i / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( j < 3 )			// Top side
					{
						var dist:Number = j * j / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( j >= Height-3 )			// Bottom side
					{
						var jOff:int = Height - j - 1;
						var dist:Number = jOff * jOff / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else if ( i >= Width-3 )			// Right side
					{
						var iOff:int = Width - i - 1;
						var dist:Number = iOff * iOff / 4;
						var alpha:int = dist * 128;
						alpha += 128;
						alpha = alpha * alpha / 128;
						if ( alpha > 255 )
							alpha = 255;
							
						alpha = alpha << 24;
						alpha &= 0xFF000000;
						Color |= alpha;
						super.pixels.setPixel32(i, j, Color);
					}
					else
					{
						Color |= 0xFF000000;
						super.pixels.setPixel32(i, j, Color);
					}
				}
			}
        }
		
        public function getId():Number {
            return id;
        }
        
		// Change opacity if on/off
		override public function update():void {
			super.update();
            if (velocity.x != 0 || velocity.y != 0) {
                // TODO: WEIRD BUG WHERE MOVED IS ALWAYS TRUE IN THE CONSTRUCTOR. FIX IT LATER AND CHANGE THE !
                if (moved && (((this.x - origX) > distance) || ((this.y - origY) > distance))) {
                    if (velocityX != 0) {
                        this.x = origX + distance;
                    } else if (velocityY != 0) {
                        this.y = origY + distance;
                    }
                    moved = !moved;
                    velocity.x = 0;
                    velocity.y = 0;
                } else if (!moved && (this.x < origX || this.y < origY)) {
                    if (velocityX != 0) {
                        this.x = origX;
                    } else if (velocityY != 0) {
                        this.y = origY;
                    }
                    moved = !moved;
                    velocity.x = 0;
                    velocity.y = 0;
                }
            }
		}
        
        public function move():void {
            var direction:Number = 1;
            // TODO: WEIRD BUG WHERE MOVED IS ALWAYS TRUE IN THE CONSTRUCTOR. FIX IT LATER AND CHANGE THE !
            if (!moved) {
                direction = -1;
            }
            velocity.x = direction * velocityX;
            velocity.y = direction * velocityY;
            FlxG.log(velocity.x);
            FlxG.log(distance);
        }
    }
}